/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javeriana.aes.loans;

import co.com.javeriana.aes.patrones.universalbank.util.ConnectDB;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;

/**
 *
 * @author Laura
 */
@WebService(serviceName = "LoansWS")
@Stateless()
public class LoansWS {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getLoans")
    public List<Loan> getLoans(@WebParam(name = "cedula") String cedula) {
        List<Loan> list = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            ConnectDB s = ConnectDB.getInstance();

            st = s.getConnection().createStatement();
            rs = st.executeQuery("select id,cedula,descripcion,valor,saldo,fechaPrestamo,fechaUltimoPago from loans where cedula=" + '"' + cedula + '"');

            list = new ArrayList<Loan>();
            while (rs.next()) {
                list.add(new Loan(rs.getInt(1), rs.getString(2), rs.getString(3), Double.toString( rs.getDouble(4)), Double.toString(rs.getFloat(5)), rs.getDate(6).toString(), rs.getDate(7).toString()));
            }
        } catch (ClassNotFoundException | SQLException e) {
            Logger.getLogger(LoansWS.class.getName()).log(Level.SEVERE, null, e);
        } finally {

            try {
                st.close();
                rs.close();
            } catch (SQLException ex) {
                Logger.getLogger(LoansWS.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return list;
    }
}
